package com.work.yshop.model;

import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 文章实体类
 */
public class Article implements Parcelable, Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private int id;                 // 文章ID
    private String title;           // 标题
    private String summary;         // 摘要
    private String content;         // HTML内容
    private String imageUrl;        // 封面图片URL
    private String publishDate;     // 发布日期
    private int viewCount;          // 阅读量
    private String category;        // 分类：热点新闻、创业项目等
    private String author;          // 作者
    
    public Article() {
    }
    
    public Article(int id, String title, String summary, String content, String imageUrl, String publishDate, int viewCount, String category) {
        this.id = id;
        this.title = title;
        this.summary = summary;
        this.content = content;
        this.imageUrl = imageUrl;
        this.publishDate = publishDate;
        this.viewCount = viewCount;
        this.category = category;
    }
    
    public Article(int id, String title, String summary, String content, String imageUrl, String author, String publishDate, int viewCount, String category) {
        this.id = id;
        this.title = title;
        this.summary = summary;
        this.content = content;
        this.imageUrl = imageUrl;
        this.author = author;
        this.publishDate = publishDate;
        this.viewCount = viewCount;
        this.category = category;
    }
    
    /**
     * 为AgriCircleFragment使用的简化构造方法
     */
    public Article(int id, String title, String summary, String imageUrl, String author, String publishDate, int viewCount, int commentCount) {
        this.id = id;
        this.title = title;
        this.summary = summary;
        this.imageUrl = imageUrl;
        this.publishDate = publishDate;
        this.viewCount = viewCount;
        // 作者和评论数不是Article的标准属性，但在这里用于显示
    }
    
    protected Article(Parcel in) {
        id = in.readInt();
        title = in.readString();
        summary = in.readString();
        content = in.readString();
        imageUrl = in.readString();
        publishDate = in.readString();
        viewCount = in.readInt();
        category = in.readString();
        author = in.readString();
    }
    
    public static final Creator<Article> CREATOR = new Creator<Article>() {
        @Override
        public Article createFromParcel(Parcel in) {
            return new Article(in);
        }
        
        @Override
        public Article[] newArray(int size) {
            return new Article[size];
        }
    };
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getSummary() {
        return summary;
    }
    
    public void setSummary(String summary) {
        this.summary = summary;
    }
    
    public String getContent() {
        return content;
    }
    
    public void setContent(String content) {
        this.content = content;
    }
    
    public String getImageUrl() {
        return imageUrl;
    }
    
    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
    
    public String getPublishDate() {
        return publishDate;
    }
    
    public void setPublishDate(String publishDate) {
        this.publishDate = publishDate;
    }
    
    public int getViewCount() {
        return viewCount;
    }
    
    public void setViewCount(int viewCount) {
        this.viewCount = viewCount;
    }
    
    public String getCategory() {
        return category;
    }
    
    public void setCategory(String category) {
        this.category = category;
    }
    
    public String getAuthor() {
        return author;
    }
    
    public void setAuthor(String author) {
        this.author = author;
    }
    
    /**
     * 获取格式化的发布时间（如：2023-06-15）
     */
    public String getFormattedPublishTime() {
        try {
            // 尝试将字符串解析为长整型时间戳
            long timestamp = Long.parseLong(publishDate);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            return sdf.format(new Date(timestamp));
        } catch (NumberFormatException e) {
            // 如果解析失败，直接返回原始日期字符串
            return publishDate;
        }
    }
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(title);
        dest.writeString(summary);
        dest.writeString(content);
        dest.writeString(imageUrl);
        dest.writeString(publishDate);
        dest.writeInt(viewCount);
        dest.writeString(category);
        dest.writeString(author);
    }
} 